package com.eqdd.yiqidian.ui.tongxunlu

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.DialogCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.utils.ToastUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCSwipeRefreshHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.fragment_relationship.*
import kotlinx.android.synthetic.main.fragment_relationship.view.*

/**
 * Created by jhj on 18-8-15.
 */
class RelationShipFragment : Fragment() {

    var friendId: String? = null


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_relationship, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var pageNum = 0
        val list = arrayListOf<Bean>()
        val mvcHelper = MVCSwipeRefreshHelper<List<Bean>>(view.swipeRefresh)
        val type = arguments?.getInt(Config.TYPE, 0)
        val guid = arguments?.getString(Config.GUID)
        val url = when (type) {
            1 -> HttpConfig.BASE_URL + HttpConfig.PEOPLE_ALUMNUS
            2 -> HttpConfig.BASE_URL + HttpConfig.PEOPLE_HOME_TOWN
            3 -> HttpConfig.BASE_URL + HttpConfig.PEOPLE_PEER
            else -> HttpConfig.BASE_URL + HttpConfig.PEOPLE_COllEAGUE
        }
        recyclerView.layoutManager = LinearLayoutManager(activity)
        recyclerView.addItemDecoration(DividerLineItemDecoration(activity))
        val slimAdapterEx = SlimAdapterEx.create()
                .register<Bean>(R.layout.list_item_184, SlimInjector<Bean> { data, injector ->
                    injector
                            ?.imageCircle(R.id.iv_avatar, data?.headimage)
                            ?.text(R.id.tv_name, data?.name)
                            ?.text(R.id.tv_info, data?.company)
                            ?.text(R.id.tv_label, data?.department + "|" + data?.post)
                            ?.clicked(R.id.tv_add) {
                                friendId = data?.userGuid
                                addFriend()
                            }
                })
                .attachTo(recyclerView)
                .updateData(list as List<Bean>?) as SlimAdapterEx<Bean>?

        val dataSource = ModelRx2DataSource<Bean>(ModelRx2DataSource.OnLoadSource<Bean> { page, register ->
            if (page == 1) {
                pageNum = 0
            }
            OkGo.post<HttpResult<List<Bean>>>(url)
                    .params("userGuid", guid)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResult<List<Bean>>>() {
                        override fun test() {
                            super.test()
                        }
                    })
                    .adapt(FlowableBody<HttpResult<List<Bean>>>())
                    .flatMap({ result ->
                        pageNum = result.page
                        if (page == 1) {
                            mvcHelper.isAutoLoadMore = true
                        }
                        val dataList = if (result.items == null) {
                            arrayListOf<Bean>()
                        } else {
                            result.items
                        }

                        Flowable.just(dataList)
                    })

        }, 10)

        mvcHelper.setDataSource(dataSource)
        mvcHelper.adapter = slimAdapterEx
        mvcHelper.refresh()
    }

    private fun addFriend() {
        val guid = arguments?.getString(Config.GUID)
        val editText = EditText(activity)
        editText.setHintTextColor(resources.getColor(R.color.hint_color))
        editText.hint = "请输入验证信息"
        activity?.let {
            AlertDialog.Builder(it)
                    .setTitle("发送好友请求")
                    .setView(editText)
                    .setPositiveButton("确定") { d, i ->
                        d.dismiss()
                        OkGo.post<HttpResult<*>>(HttpConfig.BASE_URL + HttpConfig.INVITE_FRIEND)
                                .params("userid", guid)
                                .params("friendid", friendId)
                                .params("addcontent", if (TextUtils.isEmpty(editText.text)) "" else editText.text.toString())
                                .execute(object : DialogCallBack<HttpResult<*>>(activity) {
                                    override fun onSuccess(response: Response<HttpResult<*>>) {
                                        val result = response.body()
                                        if (result.status == 200) {
                                            ToastUtil.showShort("请求成功")
                                        }
                                    }
                                })

                    }
                    .setNegativeButton("取消") { d, i -> d.dismiss() }.show()
        }
    }


    class Bean {
        val userGuid: String = ""
        val headimage: String = ""
        val company: String = ""
        val department: String = ""
        val post: String = ""
        val name: String = ""
    }
}